package com.lncd.backend.service.impl;

import com.lncd.backend.dto.GeoFenceDTO;
import com.lncd.backend.model.GeoFence;
import com.lncd.backend.model.User;
import com.lncd.backend.repository.GeoFenceRepository;
import com.lncd.backend.repository.UserRepository;
import com.lncd.backend.service.GeoFenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GeoFenceServiceImpl implements GeoFenceService {

    private final GeoFenceRepository geoFenceRepository;
    private final UserRepository userRepository;

    @Autowired
    public GeoFenceServiceImpl(GeoFenceRepository geoFenceRepository, UserRepository userRepository) {
        this.geoFenceRepository = geoFenceRepository;
        this.userRepository = userRepository;
    }

    @Override
    @Transactional
    public GeoFenceDTO createGeoFence(GeoFenceDTO geoFenceDTO) {
        User creator = userRepository.findById(geoFenceDTO.getCreatorId())
                .orElseThrow(() -> new RuntimeException("创建者不存在"));
        
        User targetUser = userRepository.findById(geoFenceDTO.getTargetUserId())
                .orElseThrow(() -> new RuntimeException("目标用户不存在"));

        GeoFence geoFence = new GeoFence();
        geoFence.setCreator(creator);
        geoFence.setTargetUser(targetUser);
        geoFence.setName(geoFenceDTO.getName());
        geoFence.setCenterLatitude(geoFenceDTO.getCenterLatitude());
        geoFence.setCenterLongitude(geoFenceDTO.getCenterLongitude());
        geoFence.setRadius(geoFenceDTO.getRadius());
        geoFence.setActive(geoFenceDTO.isActive());

        geoFence = geoFenceRepository.save(geoFence);
        return GeoFenceDTO.fromEntity(geoFence);
    }

    @Override
    public GeoFenceDTO getGeoFenceById(Long id) {
        GeoFence geoFence = geoFenceRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("地理栅栏不存在"));
        return GeoFenceDTO.fromEntity(geoFence);
    }

    @Override
    public List<GeoFenceDTO> getGeoFencesByCreatorId(Long creatorId) {
        User creator = userRepository.findById(creatorId)
                .orElseThrow(() -> new RuntimeException("创建者不存在"));
        
        return geoFenceRepository.findByCreator(creator).stream()
                .map(GeoFenceDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public List<GeoFenceDTO> getGeoFencesByTargetUserId(Long targetUserId) {
        User targetUser = userRepository.findById(targetUserId)
                .orElseThrow(() -> new RuntimeException("目标用户不存在"));
        
        return geoFenceRepository.findByTargetUser(targetUser).stream()
                .map(GeoFenceDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public GeoFenceDTO updateGeoFence(Long id, GeoFenceDTO geoFenceDTO) {
        GeoFence geoFence = geoFenceRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("地理栅栏不存在"));

        if (geoFenceDTO.getName() != null) {
            geoFence.setName(geoFenceDTO.getName());
        }
        
        if (geoFenceDTO.getCenterLatitude() != 0) {
            geoFence.setCenterLatitude(geoFenceDTO.getCenterLatitude());
        }
        
        if (geoFenceDTO.getCenterLongitude() != 0) {
            geoFence.setCenterLongitude(geoFenceDTO.getCenterLongitude());
        }
        
        if (geoFenceDTO.getRadius() > 0) {
            geoFence.setRadius(geoFenceDTO.getRadius());
        }
        
        geoFence.setActive(geoFenceDTO.isActive());

        geoFence = geoFenceRepository.save(geoFence);
        return GeoFenceDTO.fromEntity(geoFence);
    }

    @Override
    @Transactional
    public void deleteGeoFence(Long id) {
        if (!geoFenceRepository.existsById(id)) {
            throw new RuntimeException("地理栅栏不存在");
        }
        geoFenceRepository.deleteById(id);
    }

    @Override
    public List<GeoFenceDTO> getActiveGeoFencesByCreatorId(Long creatorId) {
        User creator = userRepository.findById(creatorId)
                .orElseThrow(() -> new RuntimeException("创建者不存在"));
        
        return geoFenceRepository.findByCreatorAndActive(creator, true).stream()
                .map(GeoFenceDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public List<GeoFenceDTO> getActiveGeoFencesByTargetUserId(Long targetUserId) {
        User targetUser = userRepository.findById(targetUserId)
                .orElseThrow(() -> new RuntimeException("目标用户不存在"));
        
        return geoFenceRepository.findByTargetUserAndActive(targetUser, true).stream()
                .map(GeoFenceDTO::fromEntity)
                .collect(Collectors.toList());
    }
} 